#!/bin/bash
set -e

cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1
export readonly ARCH=${1:-amd64}
export readonly NAME=${2:-$(basename "${PWD%/*}")}
export readonly VERSION=${3:-$(basename "$PWD")}


repo_url="https://spidernet-io.github.io/spiderpool"
repo_name="spiderpool/spiderpool"
chart_name="spiderpool"

rm -rf charts
mkdir charts

helm repo add ${chart_name} ${repo_url}
helm pull ${repo_name} --version=${VERSION#v} -d charts --untar

# This command is used to upgrade the versions of sriov-related components to support the arm64 architecture.
sed -i 's/tag: v1.2.0/tag: v1.3.0/g; s/tag: v2.7.0/tag: v2.8.0/g; s/tag: v1.0.2/tag: v1.1.1/g; s/tag: v3.5.1/tag: v3.7.0/g; s/tag: v1.5/tag: v1.6/g;' charts/${chart_name}/values.yaml

# This command is used to change image registory url to ghcr.nju.edu.cn to download ghcr.io's image form China 
sed -i 's/ghcr.io/ghcr.nju.edu.cn/g;' charts/${chart_name}/values.yaml

# This command is used to enable install rdmaSharedDevicePlugin
yq e -i '.rdma.rdmaSharedDevicePlugin.install="true"' charts/${chart_name}/values.yaml
